/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.product;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import javax.faces.context.FacesContext;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Category;
import org.hutech09.jsale.entity.Customer;
import org.hutech09.jsale.entity.InfoType;
import org.hutech09.jsale.entity.Price;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.entity.ProductDetail;
import org.hutech09.jsale.entity.ProductImage;
import org.hutech09.jsale.entity.ProductType;
import org.hutech09.jsale.web.customer.cusController;

/**
 *
 * @author abc
 */
@ManagedBean
@SessionScoped
public class productController implements Serializable {

    @EJB
    private ICategoryManager categoryManager;

    @EJB
    private IProductManager productManager;

    private productDetail prodetail;

    public productDetail getProdetail() {
        return prodetail;
    }

    public void setProdetail(productDetail prodetail) {
        this.prodetail = prodetail;
    }

    private List<productDetail> listProduct = new ArrayList<productDetail>();

    public List<productDetail> getListProduct() throws ItemNotFoundException {
        return listProduct;
    }

    public void setListProduct(List<productDetail> listProduct) {
        this.listProduct = listProduct;
    }

    public String getdetail(Product product) throws ItemNotFoundException {
        List<String> listimage = new ArrayList<String>();
        List<Price> prices = new ArrayList<Price>();
        this.prodetail = new productDetail();
        this.prodetail.setProduct(product);
        listimage = productManager.getImagePaths(product.getId());
        if (!listimage.isEmpty()) {
            this.prodetail.setListimage(productManager.getImagePaths(product.getId()));
            this.prodetail.setImage(prodetail.getListimage().get(0));
        } else {
            System.out.print("List Image not found");
        }
        prices = productManager.getPrices(product.getId());
        if (!prices.isEmpty()) {
            long a = (long) (productManager.getPrices(product.getId()).get(0).getVal());
            this.prodetail.setPrice(a);
        } else {
            System.out.print("List Price not found");
        }
        List<ProductDetail> details = new ArrayList<ProductDetail>();
        details = productManager.getDetail(product.getId());
        if (!details.isEmpty()) {
            this.prodetail.setListdetail(details);
        }
        return "prodetail";

    }

    public String getProduct(String protypeId) throws ItemNotFoundException {
        listProduct = new ArrayList<productDetail>();
        List<Category> listcase = new ArrayList<Category>();
        listcase = categoryManager.getall();
        List<ProductType> listProtype = new ArrayList<ProductType>();
        FacesContext context = FacesContext.getCurrentInstance();
        for (Category category : listcase) {
            System.out.printf(category.getName());
            listProtype = category.getListProtype();
            for (ProductType productype : listProtype) {
                if (productype.getId() == Long.parseLong(protypeId)) {
                    for (Product product : productype.getListProduct()) {
                        getdetail(product);
                        listProduct.add(prodetail);
                    }

                }
            }
        }
        return "home";
    }

    public String getlistbysearch(List<Product> listproduct) throws ItemNotFoundException {
        listProduct = new ArrayList<productDetail>();
        for (Product product : listproduct) {
            getdetail(product);
            listProduct.add(prodetail);
        }
        return "home";
    }

    public void getpronew() throws ItemNotFoundException {
        this.listProduct = new ArrayList<productDetail>();
        for (Product product : productManager.getNewProduct()) {
            getdetail(product);
            listProduct.add(prodetail);
        }

    }

    public productController() {
    }
}
